package feedback_handle

import (
	"peilian-api/app/global/bind"
	"peilian-api/app/global/errors"
	"peilian-api/app/model"
	"peilian-api/app/services/feedback_service"
	"peilian-api/utils/format"

	"github.com/gin-gonic/gin"
)

// 提交数据
func FeedBackSubmit(ctx *gin.Context) {
	var fbs model.FeedBackSub
	if err := bind.ShouldBinding(ctx, &fbs); err != nil {
		format.NewResponseJson(ctx).SuccessObjMsg(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	if err := new(feedback_service.FeedbackService).FeedbackSubmit(ctx, fbs); err != nil {
		format.NewResponseJson(ctx).SuccessObjMsg(err.Code, err.Err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}

// 提交图片or 视频
func FeedBackUpload(ctx *gin.Context) {
	var fbt model.FeedBackType = model.FeedBackType{}
	vpType, exsit := ctx.GetPostForm("type")
	if !exsit {
		format.NewResponseJson(ctx).SuccessObjMsg(errors.PublicErrorCode, errors.ParamBindError)
		return
	}
	fbt.Type = vpType
	out, err := new(feedback_service.FeedbackService).FeedBackUpload(ctx, fbt)
	if err != nil {
		format.NewResponseJson(ctx).SuccessObjMsg(err.Code, err.Err.Error())
		return
	}
	if out == 0 {
		format.NewResponseJson(ctx).SuccessObjMsg(err.Code, errors.DatabaseMySqlOptionFail)
		return
	}
	format.NewResponseJson(ctx).SuccessObj(
		struct {
			ID int64 `json:"id"`
		}{ID: out})
}

// 通用接口评价
func FeedBackEvaluate(ctx *gin.Context) {
	var fbi model.FeedBackInfo
	if err := bind.ShouldBinding(ctx, &fbi); err != nil {
		format.NewResponseJson(ctx).SuccessObjMsg(30001, err.Error())
		return
	}
	_, err := new(feedback_service.FeedbackService).FeedBackEvaluate(ctx, fbi)
	if err != nil {
		format.NewResponseJson(ctx).SuccessObjMsg(err.Code, err.Err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct{}{})
}
